aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/[username]/settings/+page.svelte
blob: 1451846b35e6bcccb614ca856fd76fcd5136a97e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<script>
  import { onMount } from "svelte";
	import { fly } from 'svelte/transition';
	import { enhance } from '$app/forms';
	import Account from './_account.svelte';
	import Pos from './_pos.svelte';
	import Security from './_security.svelte';

	import { Icon, Spinner, Pin } from '$comp';
	import { t } from '$lib/translations';
	import { failure, success } from '$lib/utils';
	import { avatarUpload, bannerUpload, pin } from '$lib/store';
	import { upload } from '$lib/upload';

	export let data;
	export let form;

	let { user, rates, token } = data;
	$: update(data);
	let update = () => ({ user, rates, token } = data);

	$: if (form?.message?.startsWith('Pin')) {
		failure('Wrong pin, try again');
		$pin = '';
	}

	let tab = 'account';

	let tabs = [
		{ name: 'account', key: 'ACCOUNT', comp: Account },
		{ name: 'pos', key: 'POINT_OF_SALE', comp: Pos },
		{ name: 'security', key: 'SECURITY', comp: Security }
	];

	$: ({ comp } = tabs.find((t) => t.name === tab));

	let loading, submit;
	let save = async (msg) => {
		loading = true;
		if (typeof msg !== 'string') msg = 'Settings saved';

		try {
			if ($avatarUpload) {
				await upload($avatarUpload.file, $avatarUpload.type, $avatarUpload.progress, token);
			}

			if ($bannerUpload) {
				await upload($bannerUpload.file, $bannerUpload.type, $bannerUpload.progress, token);
			}

			success(msg);
		} catch (e) {
			console.log(e);
			failure('Something went wrong');
		}

		loading = false;
	};

let loaded;
	onMount(() => setTimeout(() => (loaded = true), 50));
</script>

{#if loaded && user.haspin && $pin?.length !== 6}
	<Pin />
{/if}

<form method="POST" class="mb-[154px]" use:enhance on:submit={save}>
	<input type="hidden" name="pin" value={$pin} />

	<div class="mt-24 mb-20 px-3 md:px-0 w-full md:w-[400px] mx-auto space-y-8">
		<h1 class="text-center text-3xl md:text-4xl font-semibold mb-10">
			{$t('user.settings.header')}
		</h1>

		<div class="font-bold flex justify-between items-center border-b pb-3 text-secondary">
			{#each tabs as { name, key }}
				<button
					class="hover:opacity-80"
					class:text-black={tab === name}
					on:click|preventDefault={() => (tab = name)}>{$t(`user.settings.${key}`)}</button
				>
			{/each}
		</div>

		<svelte:component this={comp} {user} {rates} {submit} />
	</div>
	<div
		class="z-10 fixed bottom-0 bg-white shadow border-t w-full flex justify-center items-center py-3"
	>
		<button
			bind:this={submit}
			type="submit"
			class="border-2 border-black rounded-xl font-semibold mx-auto py-3 w-40 hover:opacity-80"
			class:bg-black={loading}
		>
			{#if loading}
				<Spinner />
			{:else}
				<div class="my-auto">Save Settings</div>
			{/if}
		</button>
	</div>
</form>